package com.ww.springboot.rabbitmq.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.ww.springboot.rabbitmq.dto.ExpServiceMsg;
import com.ww.springboot.rabbitmq.mq.ExchangeEnum;
import com.ww.springboot.rabbitmq.mq.MessageSender;
import com.ww.springboot.rabbitmq.mq.QueueConstants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


/**
 * @author ZH500
 */
@RestController
@RequestMapping("mq")
public class MqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MessageSender messageSender;

    private Integer i = 0;

    @RequestMapping("send")
    public String find(@RequestBody ExpServiceMsg expServiceMsg) {
        if (!StringUtils.isEmpty(expServiceMsg.getMessage())) {
            rabbitTemplate.convertAndSend(expServiceMsg.getExName(), expServiceMsg.getRoutingKey(), expServiceMsg);
        }
        return "成功";
    }

    @RequestMapping("sendDead")
    public String sendDead(@RequestBody ExpServiceMsg expServiceMsg) {
        System.out.println("发送时间:" + System.currentTimeMillis() + "-" + expServiceMsg.getBusinessCode());
        messageSender.sendRetry(QueueConstants.QueueEnum.TEST1, expServiceMsg);
        return "成功";
    }

    @RequestMapping("send2")
    public String send2() {
        ExpServiceMsg expServiceMsg = new ExpServiceMsg();
        expServiceMsg.setBusinessCode("test");
        rabbitTemplate.convertAndSend(ExchangeEnum.MQ_EXCHANGE_TEST1.getExchangeName(), QueueConstants.QueueEnum.TEST1.getRoutingKey(), expServiceMsg);
        rabbitTemplate.convertSendAndReceive(ExchangeEnum.MQ_EXCHANGE_TEST1.getExchangeName(), QueueConstants.QueueEnum.TEST1.getRoutingKey(), expServiceMsg);
//        expServiceMsg.setBusinessCode("test2");
//        rabbitTemplate.convertAndSend(ExchangeEnum.MQ_EXCHANGE_TEST2.getExchangeName(), QueueConstants.QueueEnum.TMS_ONE_KEY_COLLECT_DISPATCH_2.getRoutingKey(), expServiceMsg);
        return "成功";
    }


//    @RabbitListener(queues = QueueConstants.MQ_TEST1, concurrency = "4")
//    //@RabbitListener(queues = QueueConstants.MQ_TEST2,concurrency = "1")
//    @RabbitHandler
//    public void receive(ExpServiceMsg expServiceMsg) {
//        System.out.println("接收时间:" + System.currentTimeMillis() + "-" + expServiceMsg.getBusinessCode());
//    }

    //@RabbitListener(queues = "#{"+QueueConstants.CONTROL_QUEUE+"}", concurrency = "4")
    //@RabbitListener(queues = QueueConstants.MQ_TEST2,concurrency = "1")
    @RabbitHandler
    public void receive(@Payload Message message,
                        Channel channel) throws IOException, InterruptedException {
//        if (i % 2 == 0) {
//            //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
//            //System.out.println(channel);
//        }
//        i++;
//        ExpServiceMsg expServiceMsg = JSONUtil.toBean(new String(message.getBody()), ExpServiceMsg.class);
//        System.out.println("接收时间:" + System.currentTimeMillis() + "-" + expServiceMsg.getBusinessCode());
        Thread.sleep(5000);
        System.out.println(channel);
    }
}
